package com.alice.item.controller;

import com.alice.item.pojo.Item;
import com.alice.item.pojo.ItemForm;
import com.alice.item.pojo.ItemVo;
import com.alice.item.service.ItemService;
import com.alice.item.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by caoxuel on 2020/9/22 13:23
 */
@RestController
@RequestMapping("/item")
@CrossOrigin
@RefreshScope //刷新配置
@Api(description = "商品管理")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Value("${test.name}")
    private String name;

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询商品")
    @GetMapping("{id}")
    public Result queryById(
            @ApiParam(name = "id", value = "商品ID", required = true)
            @PathVariable Integer id){
        System.out.println("配置文件中的test.name = " + name);
        Item item = itemService.queryById(id);
        return Result.ok().data("item",item);
    }

    @GetMapping("list")
    @ApiOperation(value = "查询所有商品信息")
    public Result queryAllItem(){
        List<Item> items = itemService.queryItemList();
        return Result.ok().data("itemList",items);
    }

    @ApiOperation(value = "根据id刪除商品")
    @DeleteMapping("{id}")
    public Result deleteItem(
            @ApiParam(name = "id", value = "商品ID", required = true)
            @PathVariable Integer id){
        itemService.deleteById(id);
        return Result.ok();
    }

    @ApiOperation(value = "添加商品")
    @PostMapping("add")
    public Result addItem(
            @ApiParam(name = "itemForm", value = "添加的商品信息", required = true)
            @RequestBody ItemForm itemForm){
        Item item = new Item();

        item.setTitle(itemForm.getTitle());
        item.setStatus(itemForm.getStatus());
        item.setCid(itemForm.getCid());
        item.setNum(itemForm.getNum());
        item.setPrice(itemForm.getPrice());
        item.setSellPoint(itemForm.getSellPoint());

        item.setCreated(new Date());
        item.setUpdated(new Date());

        itemService.addItem(item);
        return Result.ok();
    }

    @ApiOperation(value = "修改商品信息")
    @PostMapping("updateItem")
    public Result updateItem(
            @ApiParam(name = "itemForm", value = "修改的商品信息", required = true)
            @RequestBody ItemForm itemForm){
        itemService.updateItem(itemForm);
        return Result.ok();
    }

    @ApiOperation(value = "分页查询商品信息")
    @PostMapping("{page}/{limit}")
    public Result queryListPage(
            @ApiParam(name = "page", value = "当前页数", required = true)
            @PathVariable Integer page,

            @ApiParam(name = "limit", value = "每页显示记录数", required = true)
            @PathVariable Integer limit,
            @ApiParam(name = "item", value = "商品查询条件", required = false)
            @RequestBody(required = false) ItemVo item){
        List<Item> items = itemService.queryPagedItem(page, limit, item);
        PageInfo pages = new PageInfo(items);
        return Result.ok().data("rows",pages.getList()).data("total",pages.getTotal());

    }
}
